package org.luckyjourney.config;

import org.luckyjourney.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

/**
 * @description:
 * @Author: Xhy
 * @CreateTime: 2023-10-24 16:27
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Autowired
    private UserService userService;

    @Value("${file.path}")
    private String filePath;

    @Value("${file.static-path}")
    private String staticPath;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AdminInterceptor(userService))
                .addPathPatterns("/admin/**", "/authorize/**")
                .addPathPatterns("/luckyjourney/**")
                .addPathPatterns("/plan/**")
                .addPathPatterns("/luckyjourney/orders/**")
                .addPathPatterns("/wechatPay/**")
                .excludePathPatterns("/luckyjourney/login/**",
                        "/luckyjourney/index/**",
                        "/luckyjourney/cdn/**",
                        "/luckyjourney/file/**",
                        "/luckyjourney/sms/**",
                        "/luckyjourney/index/video/trending/**",
                        "/luckyjourney/regular/miniprograms/**",
                        "/luckyjourney/agent/findList/**",
                        "/notify/**",
                        "/luckyjourney/NavigationBar/**",
                        "/luckyjourney/customer/listComment/{videoId}/**");

    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        String[] url = {"http://101.35.228.84:8882", "http://101.35.228.84:5378", "http://127.0.0.1:5378", "http://localhost:5378"};

        registry.addMapping("/**")
                .allowedOrigins(url)
                .allowCredentials(true)
                .allowedMethods("*")   // 允许跨域的方法，可以单独配置
                .allowedHeaders("*");  // 允许跨域的请求头，可以单独配置;
    }

    /**
     * 设置静态资源映射
     *
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler(staticPath).addResourceLocations("file:" + filePath);
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    // @Override
    // public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    //     //1.创建消息转换器对象
    //     MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    //     //2.设置消息转换对象
    //     converter.setObjectMapper(new JacksonObjectMapper());
    //     //3.添加消息转换器对象到集合中，并处于索引0的位置，会优先生效
    //     converters.add(0,converter);
    // }
}
